<template>
  <el-dialog
    :title="!dataForm.code ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    
    <el-form-item label="设备code" prop="code">
      <el-input v-model="dataForm.code" placeholder="设备code"></el-input>
    </el-form-item>
    <el-form-item label="设备名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="设备名称"></el-input>
    </el-form-item>
    <el-form-item label="设备类型" prop="categoryName">
      <el-input v-model="dataForm.categoryName" :disabled="true"></el-input>
      <!-- <el-select v-model="dataForm.categoryName" placeholder="请选择设备类型" @change="setCategoryId(dataForm.categoryName)">
        <el-option
          v-for="item in categoryList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select> -->
    </el-form-item>
    <el-form-item label="安装位置" prop="address">
      <el-input v-model="dataForm.address" placeholder="安装位置"></el-input>
    </el-form-item>
    <el-form-item label="所属村庄" prop="countryName">
      <!-- <el-input v-model="dataForm.countryName" placeholder="所属村庄"></el-input> -->
      <el-select
        v-model="dataForm.countryName"
        filterable
        remote
        reserve-keyword
        @change="setCountryId(dataForm.countryName)"
        placeholder="搜索村庄名称"
        :remote-method="remoteMethod"
        :loading="loading">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="手机号码" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="联系人手机号"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="lon">
      <el-input v-model="dataForm.lon" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="维度" prop="lat">
      <el-input v-model="dataForm.lat" placeholder="维度"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="所属组id" prop="groupId">
      <el-input v-model="dataForm.groupId" placeholder="所属组id"></el-input>
    </el-form-item>
    <el-form-item label="设备状态" prop="status">
      <el-radio-group v-model="dataForm.status">
        <el-radio :label="1">正常</el-radio>
        <el-radio :label="0">异常</el-radio>
      </el-radio-group>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        loading: false,
        dataForm: {
          id: '',
          code: '',
          name: '',
          address: '',
          description: '',
          mobile: '',
          lon: '',
          lat: '',
          remark: '',
          groupId: '',
          countryId: '',
          categoryId: '',
          countryName: '',
          categoryName: '',
          status: ''
        },
        categoryList: [],
        countryList: [],
        options: [],
        dataRule: {
          code: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          categoryName: [
            { required: true, message: '设备类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, categoryId) {
        this.dataForm.id = id || 0
        this.dataForm.categoryId = categoryId || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/tdevice/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data) {
                this.dataForm.id = data.tDevice.id
                this.dataForm.code = data.tDevice.code
                this.dataForm.name = data.tDevice.name
                this.dataForm.address = data.tDevice.address
                this.dataForm.countryId = data.tDevice.countryId
                this.dataForm.categoryId = data.tDevice.categoryId
                this.dataForm.countryName = data.tDevice.countryName
                this.dataForm.categoryName = data.tDevice.categoryName
                this.dataForm.description = data.tDevice.description
                this.dataForm.mobile = data.tDevice.mobile
                this.dataForm.lon = data.tDevice.lon
                this.dataForm.lat = data.tDevice.lat
                this.dataForm.remark = data.tDevice.remark
                this.dataForm.groupId = data.tDevice.groupId
                this.dataForm.status = data.tDevice.status
                this.dataForm.creator = data.tDevice.creator
                this.dataForm.createAt = data.tDevice.createAt
                this.dataForm.updateAt = data.tDevice.updateAt
              }
            })
          }
        })
        this.$http({
          url: this.$http.adornUrl(`/country/all`),
          method: 'get'
        }).then(({data}) => {
          if (data) {
            this.countryList = data.data.map(item => {
              return {value: `${item.id}`, label: `${item.province}${item.city}${item.county}:${item.name}`}
            })
          }
        })
        if (this.dataForm.id === 0) {
          this.$http({
            url: this.$http.adornUrl(`/devices/category/${this.dataForm.categoryId}`),
            method: 'get'
          }).then(({data}) => {
            console.log('get category' + this.dataForm.categoryId)
            if (data && data.code === 0) {
              this.dataForm.categoryName = data.data.name
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/tdevice/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id,
                'code': this.dataForm.code || undefined,
                'name': this.dataForm.name,
                'address': this.dataForm.address,
                'description': this.dataForm.description,
                'mobile': this.dataForm.mobile,
                'lon': this.dataForm.lon,
                'lat': this.dataForm.lat,
                'remark': this.dataForm.remark,
                'groupId': this.dataForm.groupId,
                'countryId': this.dataForm.countryId,
                'categoryId': this.dataForm.categoryId,
                'status': this.dataForm.status,
                'creator': this.dataForm.creator,
                'createAt': this.dataForm.createAt,
                'updateAt': this.dataForm.updateAt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      remoteMethod (query) {
        if (query !== '') {
          this.loading = true
          setTimeout(() => {
            this.loading = false
            this.options = this.countryList.filter(item => {
              return item.label.search(query) > -1
            })
          }, 200)
        } else {
          this.options = []
        }
      },
      setCountryId (countryId) {
        console.log('countryId=' + countryId)
        this.dataForm.countryId = countryId
      },
      setCategoryId (categoryId) {
        this.dataForm.categoryId = categoryId
      }
    }
  }
</script>
